package com.cms.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cms.common.StringUtils;
import com.cms.constant.CmsConstant;

import com.cms.model.TrLotteryTime;
import com.cms.model.TrLotteryVo;
import com.cms.service.LotteryService;
import com.cms.service.LotteryTimeService;
import com.cms.service.ReportService;
import com.google.gson.Gson;

@Controller
public class LotteryTimeController extends BaseController {

	@Autowired
	private LotteryTimeService lotteryTimeService;

	@Autowired
	private LotteryService lotteryService;

	@Autowired
	private ReportService reportService;

	@RequestMapping("/lotteryTime.do")
	public String enter(HttpServletRequest req, HttpServletResponse resp, ModelMap map) throws Exception 
	{
		String lotteryId = this.getActivityCode();// 获取作业的id
		int lottery = StringUtils.str2Int(StringUtils.replace(lotteryId,
				CmsConstant.LOTTERY_TIMECODE, StringUtils.EMPTY));
		TrLotteryVo vo = new TrLotteryVo();
		vo.setId(lottery);
		vo = this.lotteryService.getLotteryInfo(vo);
		map.put("lottery", vo);
		//lotteryTimeService.initLotteryTimeData(vo);
		return "lottery/time/lotteryTimeMain.html";
	}

	@RequestMapping("/lotteryTime/pagingLotteryTime.do")
	public String pagingLotteryTime(HttpServletRequest req, HttpServletResponse resp, ModelMap map) throws Exception
	{
		map.put("LotteryTimes", this.reportService.pagingReportResult(
				"pagingLotteryTime", this.buildQueryMap(req)));
		return "lottery/time/lotteryTimeList.html";
	}

	@RequestMapping("/lotteryTime/updateLotteryTime.do")
	public void updateLotteryTime(HttpServletRequest req, HttpServletResponse resp, ModelMap map) throws Exception 
	{
		int timeId = StringUtils.str2Int(req.getParameter("lotteryId"));
		String time = req.getParameter("lotteryTime");
		TrLotteryTime tr = new TrLotteryTime();
		tr.setId(timeId);
		tr.setLotteryTime(time); 
		Map<String, Object> param = new HashMap<String, Object>();
		try {
			this.lotteryTimeService.updateLotteryTime(tr);
			param.put(CmsConstant.STATUS, CmsConstant.SUCCESS_STATUS);
		} catch (Exception e) {
			param.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
			param.put(CmsConstant.MSG, e.getMessage());
		}
		this.renderText(resp, new Gson().toJson(param));
	}

}
